/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package core;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import model.Costumer;
import model.Dapstructure;
import model.Dapversion;
import model.Department;
import model.Employee;
import model.Product;
import model.Seller;
import model.Tosell;
import model.TosellId;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.ProjetHibernateUtil;
/**
 *
 * @author phinguyen
 */

 /**
     * store the hibernate session for the seller core
     */
    

public class SellerCore {
    private static Session session;

    public SellerCore() {
        session = ProjetHibernateUtil.getSessionFactory().openSession();
    }
    
public Dapstructure getCurrentDapStructure(){
        Query query = session.getNamedQuery("HQL_CURRENT_DAP_STRUCTURE");
        List list = query.list();
        Dapstructure result = (Dapstructure)list.get(0);
        return result; 
}

public void createDAP(HashMap<Product,Integer> hash, Costumer customer, String title, Seller seller ){
        
    Dapstructure structure = getCurrentDapStructure();
        Dapversion dapversion = new Dapversion(seller,customer,structure,title);
        Transaction tr = session.beginTransaction();
        session.saveOrUpdate(dapversion);
        tr.commit();
        
        for (Product entry : hash.keySet()) {
        TosellId tosellId = new TosellId(dapversion.getDvid(),entry.getPid());
        Tosell tosell = new Tosell(tosellId , entry, dapversion,(int)hash.get(entry));
        session.saveOrUpdate(tosell);
        tr.commit();        
        }
        
        
}

}